﻿#include "Merge.h"

void print(int* a, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
}
void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(n * sizeof(int));
	if (tmp == NULL)
	{
		perror("Malloc fail");
	}
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
}

void _MergeSort(int* a, int begin, int end, int* tmp)
{
	if (begin >= end)
		return;
	int mid = (begin + end) / 2;
	_MergeSort(a, begin, mid, tmp);
	_MergeSort(a, mid + 1, end, tmp);

	int begin1 = begin, end1 = mid;
	int begin2 = mid + 1, end2 = end;
	int i = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] < a[begin2])
			tmp[i++] = a[begin1++];
		else
			tmp[i++] = a[begin2++];
	}
	while (begin1 <= end1)
		tmp[i++] = a[begin1++];
	while (begin2 <= end2)
		tmp[i++] = a[begin2++];
	memcpy(a + begin, tmp + begin, sizeof(int)*(end - begin + 1));
}

//void MergeSortNonR(int* a, int n)//非标准版
//{
//	int* tmp = (int*)malloc(n * sizeof(int));
//	if (tmp == NULL)
//	{
//		perror("Malloc fail");
//	}
//	int gap = 1;
//	while (gap < n)
//	{
//		for (int i = 0; i < n; i += 2 * gap)
//		{
//			int begin1 = i, end1 = i + gap - 1;
//			int begin2 = i + gap, end2 = i + 2 * gap - 1;
//			int j = i;
//			if (begin1 < n && end1 >= n)
//			{
//				while (begin1 < n)
//					tmp[j++] = a[begin1++];
//			}
//			else if(end1 < n && begin2 >= n)
//			{
//				while (begin1 <= end1)
//					tmp[j++] = a[begin1++];
//			}
//			else if (begin2 < n && end2 >= n)
//			{
//				end2 = n - 1;
//				while (begin1 <= end1 && begin2 <= end2)
//				{
//					if (a[begin1] < a[begin2])
//						tmp[j++] = a[begin1++];
//					else
//						tmp[j++] = a[begin2++];
//				}
//				while (begin1 <= end1)
//					tmp[j++] = a[begin1++];
//				while (begin2 <= end2)
//					tmp[j++] = a[begin2++];
//			}
//			else
//			{
//				while (begin1 <= end1 && begin2 <= end2)
//				{
//					if (a[begin1] < a[begin2])
//						tmp[j++] = a[begin1++];
//					else
//						tmp[j++] = a[begin2++];
//				}
//				while (begin1 <= end1)
//					tmp[j++] = a[begin1++];
//				while (begin2 <= end2)
//					tmp[j++] = a[begin2++];
//			}
//		}
//		memcpy(a, tmp, sizeof(int) * n);
//		gap *= 2;
//	}
//	free(tmp);
//}

void MergeSortNonR1(int* a, int n)//纯修正的先全归并再拷贝
{
	int* tmp = (int*)malloc(n * sizeof(int));
	if (tmp == NULL)
	{
		perror("Malloc fail");
	}
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += 2 * gap)
		{
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			int j = i;
			if (end1 >= n)
			{
				end1 = n - 1;
				begin2 = n;
				end2 = n - 1;
			}
			else if (begin2 >= n)
			{
				begin2 = n;
				end2 = n - 1;
			}
			else if (end2 >= n)
			{
				end2 = n - 1;
			}
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
					tmp[j++] = a[begin1++];
				else
					tmp[j++] = a[begin2++];
			}
			while (begin1 <= end1)
				tmp[j++] = a[begin1++];
			while (begin2 <= end2)
				tmp[j++] = a[begin2++];
		}
		memcpy(a, tmp, sizeof(int) * n);
		gap *= 2;
	}
	free(tmp);
}

void MergeSortNonR2(int* a, int n)//归并一部分拷贝一部分
{
	int* tmp = (int*)malloc(n * sizeof(int));
	if (tmp == NULL)
	{
		perror("Malloc fail");
	}
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += 2 * gap)
		{
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			int j = i;
			if (end1 >= n || begin2 >= n)
			{
				break;
			}
			else if (end2 >= n)
			{
				end2 = n - 1;
			}
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
					tmp[j++] = a[begin1++];
				else
					tmp[j++] = a[begin2++];
			}
			while (begin1 <= end1)
				tmp[j++] = a[begin1++];
			while (begin2 <= end2)
				tmp[j++] = a[begin2++];
			memcpy(a + i, tmp + i, sizeof(int) * (end2 - i + 1));
		}
		gap *= 2;
	}
	free(tmp);
}